import { throttle } from '../untils/commonUtils';
import store from '../../../store'

export default function resizeEventHook() {
    console.log("resizeEventHook");
  var canvas = store.state.imageDragMoudules.canvas;
  var activeIsLocked = function(){
    var _a, _b;
    return (_b = (_a = store.state.imageDragMoudules.acComponent) === null || _a === void 0 ? void 0 : _a.isLocked) !== null && _b !== void 0 ? _b : false;
  };
  var throttleUpdate = throttle((newState) => {
    store.commit('updateComponentPAS', newState);
  }, 8);

  var resizeStart = function (event, point) {
      // 锁定时禁止缩放
      if (activeIsLocked) return;
      var activePoint = point;
      // 记录点所在的父元素的 状态， 与点的当前位置
      var currentPAS = {
          x: event.target.parentNode.offsetLeft,
          y: event.target.parentNode.offsetTop,
          width: event.target.parentNode.clientWidth,
          height: event.target.parentNode.clientHeight,
          clientX: event.clientX,
          clientY: event.clientY
      };
      var resizing = function (event) {
          var x = currentPAS.x, y = currentPAS.y, width = currentPAS.width, height = currentPAS.height, clientX = currentPAS.clientX, clientY = currentPAS.clientY;
          var newWidth = width;
          var newHeight = height;
          var newLeft = x;
          var newTop = y;
          var offsetX = (event.clientX - clientX) / canvas.scale; // x 方向偏移量
          var offsetY = (event.clientY - clientY) / canvas.scale; // y 方向偏移量
          // 边界判断
          var isLegalX = offsetX + x > 0 && offsetX + x < canvas.size.width;
          var isLegalY = offsetY + y > 0 && offsetY + y < canvas.size.height;
          // 根据 鼠标移动距离 设置 新的高宽
          // 左上, 均会改变
          if (activePoint === 'tl') {
              newWidth = isLegalX ? width - offsetX : width + x;
              if (newWidth > 0) {
                  newLeft = isLegalX ? x + offsetX : 0;
              }
              else {
                  newLeft = width + x;
                  newWidth = 0;
              }
              newHeight = isLegalY ? height - offsetY : height + y;
              if (newHeight > 0) {
                  newTop = isLegalY ? y + offsetY : 0;
              }
              else {
                  newTop = height + y;
                  newHeight = 0;
              }
          }
          // 中上, newLeft, newWidth 不变
          if (activePoint === 'tc') {
              newHeight = isLegalY ? height - offsetY : height + y;
              if (newHeight > 0) {
                  newTop = isLegalY ? y + offsetY : 0;
              }
              else {
                  newTop = height + y;
                  newHeight = 0;
              }
          }
          // 右上, newLeft 不变
          if (activePoint === 'tr') {
              var maxWidth = canvas.size.width - x;
              newWidth = width + offsetX > maxWidth ? maxWidth : width + offsetX;
              if (newWidth <= 0)
                  newWidth = 0;
              newHeight = isLegalY ? height - offsetY : height + y;
              if (newHeight > 0) {
                  newTop = isLegalY ? y + offsetY : 0;
              }
              else {
                  newTop = height + y;
                  newHeight = 0;
              }
          }
          // 左中, newTop、newHeight 不变
          if (activePoint === 'ml') {
              var maxWidth = width + x;
              newWidth = width - offsetX > maxWidth ? maxWidth : width - offsetX;
              newLeft = width - offsetX > maxWidth ? 0 : newLeft + offsetX;
              if (newWidth <= 0) {
                  newWidth = 0;
                  newLeft = maxWidth;
              }
          }
          // 右中, newTop、newHeight、newLeft 不变
          if (activePoint === 'mr') {
              var maxWidth = canvas.size.width - x;
              newWidth = width + offsetX < maxWidth ? width + offsetX : maxWidth;
              if (newWidth <= 0)
                  newWidth = 0;
          }
          // 下左, newTop 不变
          if (activePoint === 'bl') {
              var maxWidth = width + x;
              newWidth = width - offsetX > maxWidth ? maxWidth : width - offsetX;
              newLeft = width - offsetX > maxWidth ? 0 : newLeft + offsetX;
              if (newWidth <= 0) {
                  newWidth = 0;
                  newLeft = maxWidth;
              }
              var maxHeight = canvas.size.height - y;
              newHeight = height + offsetY < maxHeight ? height + offsetY : maxHeight;
              if (newHeight <= 0)
                  newHeight = 0;
          }
          // 下中, newWidth, newTop 不变
          if (activePoint === 'bc') {
              var maxHeight = canvas.size.height - y;
              newHeight = height + offsetY < maxHeight ? height + offsetY : maxHeight;
              if (newHeight <= 0)
                  newHeight = 0;
          }
          // 下右, newTop, newLeft 不变
          if (activePoint === 'br') {
              var maxWidth = canvas.size.width - x;
              newWidth = width + offsetX < maxWidth ? width + offsetX : maxWidth;
              if (newWidth <= 0)
                  newWidth = 0;
              var maxHeight = canvas.size.height - y;
              newHeight = height + offsetY < maxHeight ? height + offsetY : maxHeight;
              if (newHeight <= 0)
                  newHeight = 0;
          }
          // 鼠标吸附
          var gridColWidth = store.state.canvas.size.width / store.state.canvas.grid.col;
          var gridColHeight = store.state.canvas.size.height / store.state.canvas.grid.row;
          var surplusX = newLeft % gridColWidth;
          var surplusY = newTop % gridColHeight;
          if (surplusX > gridColWidth / 2) {
              newLeft = (Math.floor(newLeft / gridColWidth) + 1) * gridColWidth;
          }
          else {
              newLeft = Math.floor(newLeft / gridColWidth) * gridColWidth;
          }
          if (surplusY > gridColHeight / 2) {
              newTop = (Math.floor(newTop / gridColHeight) + 1) * gridColHeight;
          }
          else {
              newTop = Math.floor(newTop / gridColHeight) * gridColHeight;
          }
          newLeft = newLeft + 8;
          newTop = newTop + 8;
          var surplusW = newWidth % gridColWidth;
          var surplusH = newHeight % gridColHeight;
          if (surplusW > gridColWidth / 2) {
              newWidth = (Math.floor(newWidth / gridColWidth) + 1) * gridColWidth;
          }
          else {
              newWidth = Math.floor(newWidth / gridColWidth) * gridColWidth;
          }
          if (surplusH > gridColHeight / 2) {
              newHeight = (Math.floor(newHeight / gridColHeight) + 1) * gridColHeight;
          }
          else {
              newHeight = Math.floor(newHeight / gridColHeight) * gridColHeight;
          }
          newHeight = newHeight - 16;
          newWidth = newWidth - 16;
          var newPAS = {
              position: { left: Math.floor(newLeft), top: Math.floor(newTop) },
              size: {
                  width: Math.floor(newWidth) || 0,
                  height: Math.floor(newHeight) || 0
              }
          };
          throttleUpdate(newPAS);
      };
      var resizeEnd = function () {
          document.documentElement.removeEventListener('mousemove', resizing);
          document.documentElement.removeEventListener('mouseup', resizeEnd);
      };
      document.documentElement.addEventListener('mousemove', resizing);
      document.documentElement.addEventListener('mouseup', resizeEnd);
  };
  return {
      resizeStart: resizeStart
  };
}

